package com.bleeth;

import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.Setting;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class DesktopApp extends Application {

    private static final String TITLE_IMG_PATH = "file:" + System.getProperty("user.dir") + "/resource/logo.png";
    private static final String INDEX_HRML_PATH = "file:" + System.getProperty("user.dir") + "/resource/index.html";
    private static final String SETTING_PATH = System.getProperty("user.dir") + "/config/app.setting";
    private Setting setting = new Setting(SETTING_PATH);

    private static final String UAGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36";

    private Scene scene;


    @Override
    public void start(Stage primaryStage) throws Exception {
        final WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();

        String httpProxyHost = setting.getStr("http.proxyHost", "");
        String httpProxyPort = setting.getStr("http.proxyPort", "");
        if (!StrUtil.isBlank(httpProxyHost) && !StrUtil.isBlank(httpProxyPort)) {
            System.setProperty("http.proxyHost", httpProxyHost);
            System.setProperty("http.proxyPort", httpProxyPort);
        }

        browser.setContextMenuEnabled(true);
        webEngine.setUserAgent(UAGENT);
        webEngine.load(INDEX_HRML_PATH);

        int width = setting.getInt("browser.width", 1400);
        int height = setting.getInt("browser.height", 800);
        scene = new Scene(browser, width, height);

        String title = setting.getStr("browser.title", "undefine");
        primaryStage.setTitle(title);
        //设置窗口的图标.
        primaryStage.getIcons().add(new Image(TITLE_IMG_PATH));

        primaryStage.setScene(scene);

        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}
